package org.edu.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.edu.bean.Role;
import org.edu.bean.Role_user;
import org.edu.bean.User;
import org.edu.dao.RoleDao;
import org.edu.dao.UserDao;
import org.edu.service.UserService;
import org.edu.utils.StringDealUtil;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
	@Resource
	private UserDao userDao;
	@Resource
	private RoleDao roleDao;

	public User login(String systemNumber, String pass) {
		User user = userDao.getUser(systemNumber);
		if (user != null) {
			if (StringDealUtil.getMD5Str(pass).equals(user.getPassword())) {
				return user;
			}
		}
		return null;
	}

	public Integer createUser(User user) {
		return userDao.createUser(user);
	}

	public List<Role> getAllRoles() {
		return (List<Role>) roleDao.selectRoles();
	}

	public void userAddRole(int userId, String[] roleIdStrs) {
		int roleId = 0;
		Role_user roleUser;
		for (String roleStr : roleIdStrs) {
			roleId = Integer.parseInt(roleStr.trim());
			roleUser = new Role_user();
			roleUser.setUserId(userId);
			roleUser.setRoleId(roleId);
			userDao.userAddRole(roleUser);
		}
	}

	public void userAddRole(Role_user roleUser) {
		userDao.userAddRole(roleUser);
	}

	public List<User> selectUsers(User user) {

		return userDao.selectUsers(user, 0);
	}

	public void delUser(User user) {
		userDao.delUser(user);
	}

	public User selectUserByUser(int userId) {
		return userDao.selectUserById(userId);
	}

	public boolean updateUser(User user) {
		return userDao.updateUser(user);
	}

	public List<Role_user> selectRoleUsers(int userId, int roleId) {
		return userDao.selectRoleUserByUsIdRoId(userId, roleId);
	}

	public void delRole_user(Role_user roleUser) {
		userDao.delRole_user(roleUser);
	}

	public Object findById(Class clazz, int Id) {

		return userDao.findById(clazz, Id);
	}
	 

	public Object findUsInfoByUserId(int id) {
		return userDao.findUsinfoByUserId(id);
	}
	public Object saveOrUpdate(Object obj){
		return userDao.saveOrUpdate(obj);
	}
}
